package com.hundsun.yr.universal.p2p.widget;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.hundsun.yr.universal.p2p.HsApplication;

import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 验证码倒计时按钮
 * 
 * @author tonglj
 * 
 */
public class ValidateButton extends TextView implements OnClickListener {
	private long duration = 60 * 1000;// 倒计时长度,这里给了默认60秒

	// private HsApplication mApplication;

	private String textBefore = "获取验证码";
	private String textClick = "s后重新获取";
	private String textAfter = "重新发送";
	private OnClickListener mOnclickListener;
	private Timer mTimer;
	private TimerTask mTimeTask;
	private long currentTime;

	private boolean isReset = false;
	// private Map<String, Long> mTimemap = new HashMap<String, Long>();
	/**
	 * map 的key值
	 */
	private String TIME = "time";
	private String CURRENTTIME = "currenttime";

	private boolean isTime = true;

	public ValidateButton(Context context) {
		this(context, null);

	}

	public ValidateButton(Context context, AttributeSet attrs) {
		super(context, attrs);
		setOnClickListener(this);
		// mApplication = (HsApplication) context.getApplicationContext();
		// mTimemap = mApplication.getTimeMap();
	}

	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			currentTime -= 1000;
			ValidateButton.this.setText(currentTime / 1000 + textClick);
			if (currentTime <= 0) {
				ValidateButton.this.setEnabled(true);
				if (isReset) {
					ValidateButton.this.setText(textBefore);
					isReset = false;
				} else {
					ValidateButton.this.setText(textAfter);
				}
				clearTimer();
			}
		};
	};

	/**
	 * 初始化按钮
	 */
	public void initTimer() {
		currentTime = duration;
		mTimer = new Timer();
		mTimeTask = new TimerTask() {

			@Override
			public void run() {
				mHandler.sendEmptyMessage(0x01);
			}
		};
	}

	private void clearTimer() {
		if (mTimeTask != null) {
			mTimeTask.cancel();
			mTimeTask = null;
		}
		if (mTimer != null)
			mTimer.cancel();
		mTimer = null;
	}

	@Override
	public void setOnClickListener(OnClickListener l) {
		if (l instanceof ValidateButton) {
			super.setOnClickListener(l);
		} else
			this.mOnclickListener = l;
	}

	@Override
	public void onClick(View v) {
		if(isEnabled()){
			if (mOnclickListener != null)
				mOnclickListener.onClick(v);
			if (isTime) {
				initTimer();
				this.setText(currentTime / 1000 + textClick);
				this.setEnabled(false);
				mTimer.schedule(mTimeTask, 0, 1000);
			}
		}
		// mTimer.scheduleAtFixedRate(task, delay, period);
	}

	/**
	 * 和activity的onDestroy()方法同步
	 */
	public void onDestroy() {
		if (HsApplication.timeMap == null)
			HsApplication.timeMap = new HashMap<String, Long>();
		HsApplication.timeMap.put(TIME, currentTime);
		HsApplication.timeMap.put(CURRENTTIME, System.currentTimeMillis());
		clearTimer();
	}

	/**
	 * 重置函数
	 */
	public void reset() {
		if (HsApplication.timeMap != null) {
			HsApplication.timeMap.remove(TIME);
			HsApplication.timeMap.remove(CURRENTTIME);
		}
		clearTimer();
		currentTime = 0;
		isReset = true;
		setText(textBefore);
		setEnabled(true);
	}
	
//	public void resetAndClick(){
//		
//	}

	/**
	 * 设置倒计时
	 */
	public void setCountdown() {
		this.isTime = true;
//		initTimer();
//		this.setText(currentTime / 1000 + textClick);
//		this.setEnabled(false);
//		mTimer.schedule(mTimeTask, 0, 1000);
	}

	/**
	 * btn初始化
	 * @param isTime 是否点击立马倒计时  默认为true
	 * @param tag 
	 */
	public void init(boolean isTime, String tag) {
		this.isTime = isTime;
		this.CURRENTTIME = CURRENTTIME + tag;
		this.TIME = TIME + tag;

		if (HsApplication.timeMap == null || HsApplication.timeMap.size() <= 0 || !HsApplication.timeMap.containsKey(CURRENTTIME)) {
			this.setText(textBefore);
			return;
		}
		long time = System.currentTimeMillis() - HsApplication.timeMap.get(CURRENTTIME) - HsApplication.timeMap.get(TIME);
		HsApplication.timeMap.clear();
		if (time > 0) {
			this.setText(textBefore);
		} else {
			initTimer();
			this.currentTime = Math.abs(time);
			mTimer.schedule(mTimeTask, 0, 1000);
			this.setText(time + textClick);
			this.setEnabled(false);
		}
	}

	/**
	 * btn初始化
	 */
	public void init(String tag) {
		init(true, tag);
	}
	
	/**
	 * 开始计时
	 */
	public void start(){
		initTimer();
		mTimer.schedule(mTimeTask, 0, 1000);
		this.setText(currentTime + textClick);
		this.setEnabled(false);
	}
	
	/*

*
*/
}